//-------------------------------------------------------------------
//	Copyright (c) 2010-2011  Zhirnov Andrey
//	This file is part of the "UXGEN_ResourceConverter" project.
//	See copyright notice in "Converter.h".
//-------------------------------------------------------------------

#ifndef _RC_SPRITE_CONVERTER_H
#define _RC_SPRITE_CONVERTER_H

#include "stdafx.h"


class CSpriteConverter : public ISpriteConverter
{
private:
	typedef array< TSprite2D::TAnimFrame >	frames_array_t;

	struct TAnimation
	{
		string				sName;
		frames_array_t		aFrames;
		float				fFrameRate;
		///
		TAnimation() {}
		TAnimation(const string &name, float rate): sName(name), fFrameRate(rate) {}
	};

	struct TSpritePos
	{
		rectu	sPos;
		uint	uNumb;
		int8	iRotate;	// *90
		///
		TSpritePos(): uNumb(-1), iRotate(0) {}
		TSpritePos(const rectu &pos, uint numb, int8 rot): sPos(pos), uNumb(numb), iRotate(rot) {}
	};

	struct TSprImage
	{
		TImage2D				sImage;
		uvec2					sFrame;
		array< TAnimation >		aAnimations;
		string					sName;
		///
		TSprImage(const TImage2D &img, const char *name, const uvec2 &frame = uvec2(1,1)):
			sImage(img), sFrame(frame), sName(name) {}
	};

	struct TSpriteConvertInfo
	{
		uint		uWidth,
					uHeight;
		float		fEffective;
		uint		uAnimationsCount;
		///
		TSpriteConvertInfo(): uWidth(0), uHeight(0), fEffective(0.f), uAnimationsCount(0) {}
	};

private:
	TSprite2D				_sSprite;
	array< TSprImage >		_aTextures;
	string					_sInfo;
	TSpriteParams			_sParams;
	e_tex_format::type		_eFormat;
	e_tex_swizzle::type		_eSwizzle;
	bool					_bConverted;
	bool					_bGenMipmaps;
	
private:
	bool _WriteToImage(TImage2D &sImage, const array<TSpritePos> &sSprites, uint uLevel);
	bool _Convert(TSprite2D &sSprite, uvec2 &sLastDim);
	bool _CompareAnimNames(const string &sAnimName) const;

public:
	CSpriteConverter();
	~CSpriteConverter();

	// ISpriteConverter //
	virtual HRESULT CALLBACK GetSupportedFormats(char *pBuffer, uint uCount);

	virtual HRESULT CALLBACK Create();
	virtual HRESULT CALLBACK Delete();
	virtual HRESULT CALLBACK Build();
	virtual HRESULT CALLBACK Save(const char *pFileName);
	
	virtual HRESULT CALLBACK AddTexture(const char *pFileName, uint &uNumb);

	virtual HRESULT CALLBACK RemoveTexture(uint uNumb);
	virtual HRESULT CALLBACK GetTexturesCount(uint &uCount);
	virtual HRESULT CALLBACK GetTextureInfo(uint uNumb, char *pBuffer, uint uCount);

	virtual HRESULT CALLBACK GetName(uint uNumb, char *pBuffer, uint uCount);
	virtual HRESULT CALLBACK SetFrame(uint uNumb, uint uFrameX, uint uFrameY);
	virtual HRESULT CALLBACK GetFrame(uint uNumb, uint &uFrameX, uint &uFrameY);
	virtual HRESULT CALLBACK SetAnimation(uint uNumb, const char *pAnimName, float fFrameRate,
										  const TSpriteFrame *pFrames, uint uFramesCount, uint &uAnim);

	virtual HRESULT CALLBACK RemoveAnimation(uint uNumb, const char *pAnimName);
	virtual HRESULT CALLBACK RemoveAnimationN(uint uNumb, uint uAnim);
	virtual HRESULT CALLBACK GetAnimationCount(uint uNumb, uint &uCount);
	virtual HRESULT CALLBACK GetAnimation(uint uNumb, uint uAnim, char *pAnimName, uint uCharsCount, float &fFrameRate,
										  TSpriteFrame *pFrames, uint uFramesSize, uint &uFramesCount);

	virtual HRESULT CALLBACK GetInfo(char *pBuffer, uint uCount);
	
	virtual HRESULT CALLBACK GetFormat(TTextureFormat &sFormat);
	virtual HRESULT CALLBACK SetFormat(const TTextureFormat &sFormat);

	virtual HRESULT CALLBACK GetParams(TSpriteParams &sParams);
	virtual HRESULT CALLBACK SetParams(const TSpriteParams &sParams);
	
	// ISubSystem //
	virtual HRESULT CALLBACK GetType(E_SUBSYSTEM_TYPE &eType);

	IBASE_IMPLEMENTATION( ISpriteConverter )
};

#endif	// _RC_SPRITE_CONVERTER_H